<template>
  <div>
    <router-link
      tag="div"
      to="/"
      class="header-abs"
      v-show="showAbs">
      <div class="iconfont icon-back header-abs-back"></div>
    </router-link>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  props: ['posY'],
  data () {
    return {
      showAbs: true
    }
  },
  watch: {
    posY () {
      if (-this.posY && -this.posY > 60) {
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  methods: {
    handleScroll () {
      const top = document.documentElement.scrollTop
      if (top > 60) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
  },
  unmounted () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="less" scoped>
  .header-abs{
    position: absolute;
    left: .2rem;
    top: .2rem;
    width: .8rem;
    height: .8rem;
    line-height: .8rem;
    border-radius: .4rem;
    text-align: center;
    background: rgba(0, 0, 0, .6);
    .header-abs-back{
      color: #fff;
      font-size: .4rem;
    }
  }

</style>
